package com.ztsoft.user.service.project.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ztsoft.user.mapper.project.SecondBargainApproveMapper;
import com.ztsoft.user.mapper.project.SecondBargainMapper;
import com.ztsoft.user.pojo.entity.project.SecondBargain;
import com.ztsoft.user.pojo.entity.project.SecondBargainApprove;
import com.ztsoft.user.pojo.entity.user.SysUser;
import com.ztsoft.user.pojo.vo.PageData;
import com.ztsoft.user.pojo.vo.PageInfo;
import com.ztsoft.user.service.project.SecondBargainApproveService;
import com.ztsoft.user.service.project.SecondBargainService;
import com.ztsoft.user.service.user.SysUserService;
import com.ztsoft.user.utils.Result;
import com.ztsoft.user.utils.UserHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class SecondBargainApproveImpl extends ServiceImpl<SecondBargainApproveMapper, SecondBargainApprove> implements SecondBargainApproveService {
    @Resource
    private SysUserService sysUserService;

    @Resource
    private SecondBargainService secondBargainService;

    @Override
    public Result<Object> updateSecondBargainApprove(SecondBargainApprove secondBargainApprove) {
        if (updateById(secondBargainApprove)) {
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<Object> addSecondBargainApprove(SecondBargainApprove secondBargainApprove) {
        UserHolder userInfo = sysUserService.getUserInfo();
        SysUser sysUser = userInfo.getSysUser();
        secondBargainApprove.setApprovePerson(sysUser.getUserName());
        secondBargainApprove.setApproveTime(new Date());
        save(secondBargainApprove);

        if("1".equals(secondBargainApprove.getApproveStatus())) {
            //通过
            secondBargainService.access(secondBargainApprove.getPid());
        } else if("2".equals(secondBargainApprove.getApproveStatus())) {
            secondBargainService.reject(secondBargainApprove.getPid());
        }

        return Result.success();
    }

    @Override
    public Result<Object> deleteSecondBargainApprove(String id) {
        if (removeById(id)) {
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<Object> querySecondBargainApprove(String pid, Integer currentPage, Integer pageSize) {
//        QueryWrapper<SecondBargain> secondBargainQueryWrapper = new QueryWrapper<>();
//        if (ObjectUtil.isNotNull(projectId)) {
//            secondBargainQueryWrapper.eq("project_id", projectId);
//        }
//
//        Page<SecondBargain> page = new Page<>(currentPage, pageSize);
//        Page<SecondBargain> secondBargainPage = page(page, secondBargainQueryWrapper);
//        List<SecondBargain> secondBargainList = secondBargainPage.getRecords();
//
//        return Result.success(new PageData(secondBargainList, new PageInfo(pageSize, currentPage, (int) secondBargainPage.getTotal())));
        return null;
    }
}
